<template>
	<view class="toPayInfo">
		<!-- 顶部标题头 -->
		<view class="top-title-box">
			<view class="title-text-box" :style="'padding-top:'+titleHeight+'rpx;'">
				<view class="back_pages"  @click="backPage">
					<u-icon style="margin-top: -5rpx;" size="30" name="arrow-leftward"></u-icon>
				</view>
				<view class="title-name-text">
					{{orderData.itemList[0].skuName}}
				</view>
			</view>
		</view>
		<!-- body 数据板块 -->
		<view class="body-center-box">
			<view class="warn-title-text">
				支付剩余时间
			</view>
			<view class="countdown-body-box">
				<uni-countdown ref='countdown' :font-size="32" color="#000000" background-color="#e9e9eb" border-color="#909399" :showDay="false"
				  :hour="0" :minute="countDown" :second="secondTime" @timeup="timeout"></uni-countdown>
			</view>
		</view>
		<!-- 提醒板块 -->
		<view class="count-number-text">
			请在30分钟内完成支付， 订单超时将自动关闭！
		</view>
		
		<!-- 支付金额 -->
		<view class="pay-number-text">
			订单应付金额： <span style="font-size: 42rpx; font-weight: bold; color: red;">￥{{orderData.payAmount}}</span>
		</view>
		
		<!-- 按钮 -->
		<button :disabled="disabled" class="pay-bottom" @click="goPay">微信支付 ￥ {{orderData.payAmount}}</button>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderData: {},
				titleHeight: '',
				disabled: false,
				countDown: 0,
				secondTime: 0,
				showModal: false,
				orderType: 1
			}
		},
		onLoad(option) {
			const that = this
			const eventChannel = this.getOpenerEventChannel()
			eventChannel.on('item', function(data){
				console.log(data);
				that.orderData = data
				// 根据订单号，获取 redis 该订单的支付倒计时
				that.getCountTime(data.orderSn)
				that.getOrderType(data)
			})
			this.getPhoneHeightStatus()
		},
		methods: {
			// 获取导航栏的高度
			/**
			 * 获取手机状态栏高度
			 */
			getPhoneHeightStatus() {
				let systemInfo = uni.getSystemInfoSync();
				let pxToRpxScale = 750 / systemInfo.windowWidth; //px转换到rpx的比例
				let fontSize = systemInfo.fontSizeSetting * pxToRpxScale; // 用户设置的字体大小
				let statuHeight = systemInfo.statusBarHeight * pxToRpxScale; //状态栏的高度
				let titleHeight = 25 * pxToRpxScale; //导航栏高度，这个一般是固定的
				this.titleHeight = titleHeight
			},
			// 返回上一级页面
			backPage() {
				uni.navigateBack({
					delta: 1
				})
			},
			
			// 倒计时到点触发
			timeout(){
				// 1、禁用按钮
				this.disabled = true
				// 2、更改订单状态
			},
			// 获取订单类型
			getOrderType(item){
				const that = this
				this.$api.request.getOrderInfoType({
					memberId: item.memberId,
					orderId: item.id,
				}, res => {
					console.log(res);
					if (res.code == 200) {
						// 如果该订单是商品: 1  服务 :2
						that.orderType = res.data
					}
				})
			},
			
			// 获取倒计时时间
			getCountTime(orderSn){
				this.$api.request.getRedisCountDown({orderId: orderSn}, res => {
					console.log(res);
					if(res.code == 200){
						this.countDown = Math.floor(res.data / 60),
						this.secondTime = res.data % 60
						if(res.data / 60 == 0 && res.data % 60 == 0){
							this.disabled = true
						}
						this.disabled = false
					}
				})
			},
			
			// 点击支付
			goPay() {
			  console.log(this.orderData);
			  try {
				  let data;
				// 判断库存是否足够
				for(let i = 0; i < this.orderData.itemList.length; i++){
					this.orderData.itemList[i].quantity = this.orderData.itemList[i].skuQuantity
				}
				data = this.orderData.itemList
				
				this.$api.request.isEnoughStatus(data, res => {
					if(res.code == 200 && res.data == true){
						this.$api.request.memberSubmitOrder(this.orderData, res => {
						  this.toPay(res);
						})
					}else {
						setTimeout(() => {
							this.$api.msg("库存不足")
							setTimeout(() => {
								this.disabled = true
							}, 2000)
						}, 1000)
					}
				})
				
			    
			  } catch (e) {
			    console.log(e);
			  }
			},
			toPay(data) {
			  let res = JSON.parse(data.data.jsonStr)
			console.log(data);
			  uni.requestPayment({
			    timeStamp: res.timeStamp,
			    nonceStr: res.nonceStr,
			    package: res.package,
			    signType: res.signType,
			    paySign: res.paySign,
			    success: (res) => {
			      this.$api.request.payNotify(data.data.user, res => {
			        console.log("通知", res);
			        uni.navigateBack({
			        	delta: 1
			        })
			      })
			
			    },
			    fail: (err) => {
			      console.log(err);
			      this.$api.msg("支付失败！")
			      // uni.navigateTo({
			      // 	url: '/pagesA/sys/user/order?type=0'
			      // });
			    }
			  })
			},
		}
	}
</script>

<style lang="scss">
	.toPayInfo{
		width: 100vw;
		height: 100vh;
		background: #FFFFFF;
	}
	
	.title-text-box{
		width: 100vw;
		height: 60rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		position: relative;
	}
	
	.title-name-text{
		font-size: 36rpx;
		font-weight: 500;
		margin: 0 auto;
	}
	
	.back_pages {
		width: 60rpx;
		height: 60rpx;
		border-radius: 60rpx;
		background: rgba(0, 0, 0, 0.4);
		color: #FFFFFF;
		display: flex;
		justify-content: center;
		align-items: center;
		position: absolute;
		left: 30rpx;
		z-index: 99999;
	}
	
	.body-center-box{
		width: 100vw;
		display: flex;
		flex-direction: column;
	}
	
	.warn-title-text{
		width: 100vw;
		text-align: center;
		font-size: 32rpx;
		font-weight: bold;
		margin-top: 120rpx;
	}
	
	.countdown-body-box{
		width: 100vw;
		display: flex;
		justify-content: center;
		margin-top: 70rpx;
	}
	
	.uni-countdown__number{
		font-size: 42rpx !important;
		padding: 18rpx 24rpx !important;
		border-radius: 10rpx !important;
	}
	
	.uni-countdown__splitor{
		line-height: 70rpx !important;
		font-size: 70rpx !important;
	}
	
	.count-number-text{
		width: 100vw;
		font-size: 28rpx;
		text-align: center;
		margin-top: 60rpx;
	}
	
	.pay-number-text{
		width: 100vw;
		font-size: 34rpx;
		text-align: center;
		margin-top: 20vh;
	}
	
	.pay-bottom{
		width: auto;
		margin: 0 60rpx;
		margin-top: 50rpx;
		background: #3a3a3a;
		color: #FFFFFF;
		border-radius: 5rpx !important;
	}
</style>
